﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Net.Configuration;
using System.Configuration;
using Medianamik.Core.Sugar;

namespace Medianamik.Core.Mail
{
    public class MailDefinition
    {
        public MailDefinition() { }

        #region Properties
        public string BodyResourceName
        {
            get;
            set;
        }
        public string BodyFilePath
        {
            get;
            set;
        }
        public string Subject
        {
            get;
            set;
        }

        public bool IsBodyHtml
        {
            get;
            set;
        }

        public string From
        {
            get;
            set;
        }
        
        #endregion

        #region Public Methods

        public MailMessage CreateMailMessage(string[] pRecipients, IDictionary<string,string> pReplacements, string pBody  )
        {
            var recipients = pRecipients.Where(r => !String.IsNullOrEmpty(r));

            if(recipients.Count() < 1)
                throw new CoreException("No recipients specified");

            var from = this.From;
            if (string.IsNullOrEmpty(from))
            {
                SmtpSection smtp = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                if (((smtp == null) || (smtp.Network == null)) || string.IsNullOrEmpty(smtp.From))
                {
                    throw new CoreException("No From address specified");
                }
                from = smtp.From;
            }

            var body = pBody;
            if (pReplacements != null)
            {
                foreach (var replacement in pReplacements)
                {
                    body = Utility.FastReplace(body, replacement.Key, replacement.Value, StringComparison.Ordinal);
                }
            }
                       
            var message = new MailMessage()
            {
                Subject = this.Subject,
                Body = body,
                IsBodyHtml = this.IsBodyHtml,
                From = new MailAddress(from) ,

            };
            foreach (var recipent in recipients)
            {
               message.To.Add(recipent);
            }

            return message;

        }

        public MailMessage CreateMailMessage(string[] pRecipients, IDictionary<string, string> pReplacements)
        {
            string mailBody;

            if (!string.IsNullOrEmpty(BodyFilePath))
            {
                if (!Utility.GetFileContentFromPath(BodyFilePath, out mailBody))
                    throw new CoreException("A physical email template file is specified but was not found : " + BodyFilePath); 
            }
            else if (!string.IsNullOrEmpty(BodyResourceName))
                mailBody = Sugar.Resources.GetResourceText(BodyResourceName, Assembly.GetExecutingAssembly());
            else
                throw new CoreException("Unable to get the body template value"); 

            return CreateMailMessage(pRecipients, pReplacements, mailBody);
        }

        

        #endregion


    }
}
